package com.baizhi.controller;

import com.baizhi.entity.Article;
import com.baizhi.entity.Guru;
import com.baizhi.service.ArticleService;
import com.baizhi.service.GuruService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {

    ///把字符串转换成日期格式
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值
    }

    @Autowired
    private ArticleService articleService;

    //文章的查询  分页+模糊
    @RequestMapping("/listArticle")
    public Map listArticle(int page, int limit, String name){
        return articleService.listArticle(page,limit,name);
    }

    //发布文章
    @RequestMapping("/insertArticle")
    public Map insertArticle(Article article,MultipartFile imageUrl, HttpServletRequest request) throws IOException {
        System.out.println(article);
        String realPath = request.getServletContext().getRealPath("/img");
        //重命名，防止同名文件被覆盖
        String newFileName = new Date().getTime() + "_" + imageUrl.getOriginalFilename();
        imageUrl.transferTo(new File(realPath + "/" + newFileName));

        article.setArticleImage("http://localhost:8989/cmfzdemo/img/"+imageUrl.getOriginalFilename());

        return articleService.insertArticle(article);
    }

    @Autowired
    private GuruService guruService;

    //查询文章对象
    @RequestMapping("/selectArtById")
    public Map selectArtById(Integer id){
        Article selectArtById = articleService.selectArtById(id);
//        List<Guru> listGuruNick = guruService.listGuruNick();
        Map map=new HashMap();
        map.put("selectArtById",selectArtById);
//        map.put("listGuruNick",listGuruNick);
        return map;
    }

    //修改文章
    @RequestMapping("/updateArticle")
    public Map updateArticle(Article article,MultipartFile imageUrl, HttpServletRequest request) throws IOException {

        String realPath = request.getServletContext().getRealPath("/img");
        //重命名，防止同名文件被覆盖
        String newFileName = new Date().getTime() + "_" + imageUrl.getOriginalFilename();
        imageUrl.transferTo(new File(realPath + "/" + newFileName));

        article.setArticleImage("http://localhost:8989/cmfzdemo/img/"+imageUrl.getOriginalFilename());
        return articleService.updateArticle(article);
    }

    //删除文章
    @RequestMapping("/deleteArticle")
    public Map deleteArticle(Integer id){
        return articleService.deleteArticle(id);
    }

    //批量上传文章-批量导入
    @RequestMapping("/multiInsertArticle")
    public void multiInsertArticle(){

    }
    //批量下载文章-批量导出
    @RequestMapping("/multiDownloadArticle")
    public void multiDownloadArticle(){

    }
}
